<!DOCTYPE html>
<html>

<head>

  <title>Peers javascript demo page</title>

  <style type="text/css">

  #logger, #networkLogger {
    border: 1px solid black;
    width: 45%;
    height: 300px;
    float: left;
    margin: 10px;
    overflow: auto;
    white-space: pre-line;
  }

  #account {
    border: none;
  }

  #incomingCall {
    visibility: hidden;
  }

  </style>

  <script type="text/javascript">

  var inviteSipRequest;

  function localLog(prefix, message, divId) {
    var logger = document.getElementById(divId);
    var line = "";
    if (prefix) {
      line = prefix + " ";
    }
    line += message;
    var node = document.createTextNode(line);
    logger.appendChild(node);
    node = document.createElement("br");
    logger.appendChild(node);
  }

  function javaLog(message) {
    localLog("java", message, "logger");
  }

  function javaNetworkLog(message) {
    localLog(null, message, "networkLogger");
  }

  function log(message) {
    var date = new Date();
    var line = date.getFullYear() + "-" + (date.getMonth() + 1) + "-";
    line += date.getDate() + " " + date.getHours() + ":" + date.getMinutes();
    line += ":" + date.getSeconds() + "," + date.getMilliseconds();
    line += " " + message;
    localLog("js", line, "logger");
  }

  // server functions

  function registering(sipRequest) {
    log("registering");
  }

  function registerSuccessful(sipResponse) {
    log("registerSuccessful " + sipResponse.getStatusCode());
  }

  function registerFailed(sipResponse) {
    log("registerFailed " + sipResponse);
    var registerActionInput = document.getElementById("registerAction");
    registerActionInput.value = "register";
  }

  function incomingCall(sipRequest, provResponse) {
    log("incomingCall");
    inviteSipRequest = sipRequest;
    var incomingCallDiv = document.getElementById("incomingCall");
    var callerDiv = document.getElementById("caller");
    var peersObj = document.getElementById("peers");
    var fromStr = peersObj.getFrom(sipRequest);
    var fromTxt = document.createTextNode(fromStr);
    callerDiv.appendChild(fromTxt);
    incomingCallDiv.style.visibility = "visible";
  }

  function remoteHangup(sipRequest) {
    log("remoteHangup");
    var callActionInput = document.getElementById("callAction");
    callActionInput.value = "call";
    // cleanup for incoming calls cancelled
    var incomingCallDiv = document.getElementById("incomingCall");
    incomingCallDiv.style.visibility = "hidden";
    var callerDiv = document.getElementById("caller");
    callerDiv.innerHTML = "";
  }

  function ringing(sipResponse) {
    log("ringing");
  }

  function calleePickup(sipResponse) {
    log("calleePickup");
  }

  function error(sipResponse) {
    log("error " + sipResponse);
    var callActionInput = document.getElementById("callAction");
    callActionInput.value = "call";
  }

  function setInviteSipRequest(sipRequest) {
    log("setInviteSipRequest " + sipRequest);
    inviteSipRequest = sipRequest;
  }

  // client functions

  function doInstantiatePeersAction() {
    log("doInstantiatePeersAction");
    var peersObj = document.getElementById("peers");
    var ipAddressInput = document.getElementById("ipAddress");
    peersObj.instantiatePeers(ipAddress.value);
  }

  function doRegisterAction() {
    log("doRegisterAction");
    var peersObj = document.getElementById("peers");
    var registerActionInput = document.getElementById("registerAction");
    if (registerActionInput.value == "register") {
      var userPartInput = document.getElementById("userPart");
      var domainInput = document.getElementById("domain");
      var passwordInput = document.getElementById("password");
      var outboundProxyInput = document.getElementById("outboundProxy");
      registerActionInput.value = "unregister";
      peersObj.register(userPartInput.value, passwordInput.value,
          domainInput.value, outboundProxyInput.value);
    } else {
      registerActionInput.value = "register";
      peersObj.unregister();
    }
  }

  function doCallAction() {
    log("doCallAction");
    var peersObj = document.getElementById("peers");
    var callActionInput = document.getElementById("callAction");
    if (callActionInput.value == "call") {
      var calleeInput = document.getElementById("callee");
      callActionInput.value = "hangup";
      peersObj.invite(calleeInput.value);
    } else {
      callActionInput.value = "call";
      peersObj.terminate(inviteSipRequest);
    }
  }

  function doReject() {
    log("doReject");
    var peersObj = document.getElementById("peers");
    peersObj.busyHereClicked(inviteSipRequest);
    var incomingCallDiv = document.getElementById("incomingCall");
    incomingCallDiv.style.visibility = "hidden";
    var callerDiv = document.getElementById("caller");
    callerDiv.innerHTML = "";
  }

  function doPickup() {
    log("doPickup");
    var peersObj = document.getElementById("peers");
    peersObj.pickupClicked(inviteSipRequest);
    var incomingCallDiv = document.getElementById("incomingCall");
    incomingCallDiv.style.visibility = "hidden";
    var callerDiv = document.getElementById("caller");
    callerDiv.innerHTML = "";
    var callActionInput = document.getElementById("callAction");
    callActionInput.value = "hangup";
  }
  </script>

</head>

<body>
    <script src="deployJava.js"></script>
    <script type="text/javascript">
        var attributes = { id:'peers', code:'net.sourceforge.peers.javascript.JsUserAgent',  width:1, height:1} ;
        var parameters = {jnlp_href: 'applet-tests.jnlp'} ;
        deployJava.runApplet(attributes, parameters, '1.6');
    </script>
    <div>
      warning: this demo will only work if you register on a registrar (pabx)
      on your local network. This limitation is due to restrictions on
      permissions when java runtime environment is run as a browser plugin.
    </div>
    <div>
      local ip address:
      <input id="ipAddress" type="text"/>
      (use this field if your computer has several local area network ip
      adresses). In any case, click "init" first.
    </div>
    <div>
      <input id="instantiatePeersAction" type="button" value="init" onclick="doInstantiatePeersAction()"/>
    </div>
    <table id="account">
      <tbody>

        <tr>
          <td>user part:</td>
          <td><input id="userPart" type="text"/></td>
          <td></td>
        </tr>
        <tr>
          <td>domain:</td>
          <td><input id="domain" type="text"/></td>
          <td></td>
        </tr>
        <tr>
          <td>password:</td>
          <td><input id="password" type="password"/></td>
          <td></td>
        </tr>
        <tr>
          <td>outbound proxy:</td>
          <td><input id="outboundProxy" type="text"/></td>
          <td>(example: sip:192.168.1.20;lr)</td>
        </tr>
      </tbody>
    </table>
    <div>
      <input id="registerAction" type="button" value="register" onclick="doRegisterAction()"/>
    </div>
    <div>
      callee: <input id="callee" type="text"/> (example: sip:6002@asterisk-ip)
    </div>
    <div>
      <input id="callAction" type="button" value="call" onclick="doCallAction()"/>
    </div>
    <div id="incomingCall">
      incoming call from <div id="caller"></div>
      <input id="reject" type="button" value="reject" onclick="doReject()"/>
      <input id="pickup" type="button" value="pickup" onclick="doPickup()"/>
    </div>
    <div id="logger"></div>
    <div id="networkLogger"></div>
</body>

</html>

